//
//  SportLocusVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/31.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "SportLocusVC.h"
#import "MBTUiColor.h"
#import <MAMapKit/MAMapKit.h>
#import "HttpTool.h"
#import "SportStatus.h"
#import "SportStausSucess.h"
#import "MJExtension.h"
#import "DataTool.h"
#import "ShareView.h"

#define bgColor @"#1186D6"

@interface SportLocusVC ()<UIGestureRecognizerDelegate,MAMapViewDelegate>{
    NSMutableArray *arrayMap;
    MAPolyline *commonPolyline;
    ShareView *shareView;
}
@property (weak, nonatomic) IBOutlet UIView *staus;
@property (weak, nonatomic) IBOutlet UIView *nav;
@property (weak, nonatomic) IBOutlet UIView *left;
@property (weak, nonatomic) IBOutlet UIView *right;

@property (weak, nonatomic) IBOutlet UILabel *distance;
- (IBAction)shareSport:(id)sender;
- (IBAction)sportData:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *slowest;
@property (weak, nonatomic) IBOutlet UILabel *faster;
@property (weak, nonatomic) IBOutlet UILabel *pace;
@property (weak, nonatomic) IBOutlet UILabel *timeLong;
@property (weak, nonatomic) IBOutlet UILabel *speed;
@property (weak, nonatomic) IBOutlet UILabel *heartrate;
@property (weak, nonatomic) IBOutlet UILabel *calorie;
@property (weak, nonatomic) IBOutlet UILabel *steps;
@property (weak, nonatomic) IBOutlet MAMapView *map;
@property (weak, nonatomic) IBOutlet UIView *dataView;

@end

@implementation SportLocusVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initMapView];
    shareView =[ShareView headerView];
    [self.map addSubview:shareView];
    arrayMap =[NSMutableArray array];
    [self setViewBG];
    [self backToRunVC];
    [self getSportStatistics];
    [self getSportPath];
    
    
    /* Polyline. */
    CLLocationCoordinate2D commonPolylineCoords[5];
    commonPolylineCoords[0].latitude = 39.832136;
    commonPolylineCoords[0].longitude = 116.34095;
    
    commonPolylineCoords[1].latitude = 39.832136;
    commonPolylineCoords[1].longitude = 116.42095;
    
    commonPolylineCoords[2].latitude = 39.902136;
    commonPolylineCoords[2].longitude = 116.42095;
    
    commonPolylineCoords[3].latitude = 39.902136;
    commonPolylineCoords[3].longitude = 116.44095;
    
    commonPolylineCoords[4].latitude = 39.932136;
    commonPolylineCoords[4].longitude = 116.44095;
    
   commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:5];
    [arrayMap addObject:commonPolyline];
    //在地图上添加折线对象
}
-(void)setViewBG{
    self.navigationController.navigationBarHidden=YES;
    _staus.backgroundColor =[UIColor colorwithHexString:bgColor];
    _nav.backgroundColor =[UIColor colorwithHexString:bgColor];
    _left.backgroundColor =[UIColor colorwithHexString:bgColor];
    _right.backgroundColor =[UIColor colorwithHexString:bgColor];
}

-(void)backToRunVC{
    _left.userInteractionEnabled = YES;
    UITapGestureRecognizer *backTapGest = [[UITapGestureRecognizer alloc] init];
    backTapGest.numberOfTapsRequired = 1;
    backTapGest.delegate = self;
    [_left addGestureRecognizer:backTapGest];
    [backTapGest addTarget:self action:@selector(backTapView:)];
}
- (void)backTapView:(UITapGestureRecognizer *)backTapGest{
    [self.navigationController popViewControllerAnimated:YES];//返回
}

-(void)getSportStatistics{//算法不容易啊
    NSDictionary *params = @{@"user_id":@"phone13480151236"};
    [HttpTool GET:@"http://www.aiaiaini.com/demo/web/index.php?r=api/sport/get-statistics" WithHeader:NO parameters:params success:^(id responseObject) {
        SportStausSucess *data =[SportStausSucess mj_objectWithKeyValues:responseObject];
        if (data.status) {
            SportStatus *staus =data.data;
            float d =(float)staus.run_distance/1000;
            NSLog(@"d====== %f",d);
            _distance.text =[NSString stringWithFormat:@"%.2f",d];
            
            _faster.text=[NSString stringWithFormat:@"%i",staus.run_max_speed];
            _slowest.text=[NSString stringWithFormat:@"%i",staus.run_min_speed];
            float m =(float)staus.run_time/60;//分钟
             NSLog(@"m====== %f",m);
            float min =m/d;
             NSLog(@"min====== %f",min);
           NSArray *arry = [[NSString stringWithFormat:@"%f",min] componentsSeparatedByString:@"."];
            NSLog(@"arrrry====%@",arry[0]);
           float second = min -[arry[0] floatValue];
           NSString *se = [NSString stringWithFormat:@"%.2f", second*60];//分钟
            _pace.text=[NSString stringWithFormat:@"%@%@%@%@",arry[0],@"'",se,@"'"];
            _timeLong.text=[DataTool tampChangeToHour:staus.run_time];
            float t = (float)staus.run_time/3600;
            _speed.text=[NSString stringWithFormat:@"%.2f",d/t];
            //_heartrate.text=@"";
            _calorie.text=[NSString stringWithFormat:@"%i",staus.run_calorie];
            _steps.text=[NSString stringWithFormat:@"%i",staus.walk_step];
        }else{
            //信息出错
        }
    } failure:^(NSError *error){
        NSLog(@"%@",error);
    }];
}
-(void)getSportPath{
    int sid =[_info.sid intValue];
    int sport_key =[_info.sport_key intValue];
    NSDictionary *params = @{@"user_id":@"phone13480151236",@"sid":[NSNumber numberWithInt:sid],@"sport_key":[NSNumber numberWithInt:sport_key],@"page":@1,@"pageSize":@10};
    [HttpTool GET:@"http://www.aiaiaini.com/demo/web/index.php?r=api/sport/get-paths" WithHeader:NO parameters:params success:^(id responseObject) {

    } failure:^(NSError *error){
        NSLog(@"%@",error);
    }];
}
//原来是这个不对啊
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
        NSLog(@"MAPolyline＝＝＝＝＝＝＝＝＝＝＝＝");
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        polylineRenderer.lineWidth    = 8.f;
        polylineRenderer.strokeColor  = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.6];
        polylineRenderer.lineJoinType = kMALineJoinRound;
        polylineRenderer.lineCapType  = kMALineCapRound;
        return polylineRenderer;
    }
    return nil;
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    shareView.frame=_dataView.frame;//随便设定一个frame
     shareView.alpha=0;
}
- (IBAction)shareSport:(id)sender {
    [UIView animateWithDuration:1.0 // 动画时长
                          delay:0.0 // 动画延迟
         usingSpringWithDamping:0.5// 类似弹簧振动效果 0~1
          initialSpringVelocity:3.0 // 初始速度
                        options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                     animations:^{
                         _dataView.frame =CGRectMake(_dataView.frame.size.width,_dataView.frame.origin.y,_dataView.frame.size.width, _dataView.frame.size.height);
                     } completion:^(BOOL finished) {
                          _dataView.hidden=YES;
                     }];
    
    
    [UIView animateWithDuration:1.0 // 动画时长
                          delay:0.0 // 动画延迟
         usingSpringWithDamping:1.0 // 类似弹簧振动效果 0~1
          initialSpringVelocity:1.0 // 初始速度
                        options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                     animations:^{
                         shareView.alpha=1;
                     } completion:^(BOOL finished) {
                         
                     }];
//    [UIView animateWithDuration:1.0 animations:^{
//        _dataView.frame =CGRectMake(0,_map.frame.size.height, 0, 0);
//    } completion:^(BOOL finished) {
//         _dataView.hidden=YES;
//    }];
    
//    [UIView animateWithDuration:1.0 animations:^{
//        shareView.alpha=1;
//    } completion:^(BOOL finished) {
//    
//    }];
    
//做一些动画
}

- (IBAction)sportData:(id)sender {
    _dataView.hidden=NO;
    [UIView animateWithDuration:0.5 animations:^{
        shareView.alpha=0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 // 动画时长
                              delay:0.0 // 动画延迟
             usingSpringWithDamping:0.5// 类似弹簧振动效果 0~1
              initialSpringVelocity:3.0 // 初始速度
                            options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                         animations:^{
                             _dataView.frame =CGRectMake(0,_dataView.frame.origin.y,_dataView.frame.size.width, _dataView.frame.size.height);
                         } completion:^(BOOL finished) {
                             
                         }];
    }];
    
}

- (void)initMapView
{
    _map.delegate=self;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [_map addOverlays:arrayMap];
    const CGFloat screenEdgeInset = 40;
    UIEdgeInsets inset = UIEdgeInsetsMake(screenEdgeInset, screenEdgeInset, screenEdgeInset, screenEdgeInset);
    [_map setVisibleMapRect:commonPolyline.boundingMapRect edgePadding:inset animated:NO];
}
@end
